<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="事项名称" prop="matterName">
        <el-input v-model="pram.matterName" maxlength="500" placeholder="事项名称" />
      </el-form-item>
      <el-form-item label="责任单位" prop="unit">
        <el-input v-model="pram.unit" maxlength="150" placeholder="责任单位" />
      </el-form-item>
      <el-form-item label="责任书记" prop="dutyName">
        <el-select v-model="pram.dutyName" placeholder="责任书记" clearable filterable default-first-option @blur="onTypeBlur($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in dutyNameList.list"
            :key="index"
            :label="item.name + ' ' + item.duties"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职务" prop="dutiesId">
        <el-select v-model="pram.dutiesId" placeholder="职务" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in positionList.list"
            :key="index"
            :label="item.name + ' ' + item.duties"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="pram.startTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placement="bottom-end" @change="onchangeTime" placeholder="开始时间"/>
      </el-form-item>
      <el-form-item label="完成时限" prop="completeTime">
        <el-date-picker v-model="pram.completeTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placement="bottom-end" @change="onchangeTime1" placeholder="完成时限"/>
      </el-form-item>
    </el-form>
    <div class="btn-box">
      <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:supervise:supervision:save','admin:supervise:supervision:info']">{{ isCreate===0?'确定':'更新' }}</el-button>
      <el-button @click="close">取消</el-button>
    </div>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import  * as superviseApi from '@/api/superviseApi.js'
import * as roleApi from '@/api/role.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { Tinymce },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allPositionList:{
      type:Object
    },
    allDutyNameList:{
      type:Object
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        matterName: null,
        unit: null,
        dutiesId:null,
        startTime: null,
        dutyName:null,
        completeTime: null,
        id: 0,
      },
      rules: {
        matterName: [{ required: true, message: '请填事项名称', trigger: ['blur', 'change'] }],
        unit: [{ required: true, message: '请填责任单位', trigger: ['blur', 'change'] }],
        dutiesId: [{ required: true, message: '请选择职务', trigger: ['blur', 'change'] }],
        startTime: [{ required: true, message: '请选择开始日期', trigger: ['blur', 'change'] }],
        dutyName: [{ required: true, message: '请选择责任书记', trigger: ['blur', 'change'] }],
        completeTime: [{ required: true, message: '请选择完成时限', trigger: ['blur', 'change'] }],
      },
      positionList:{},
      dutyNameList:{},
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeTime (e) {
      this.pram.startTime = e;
    },
    onchangeTime1 (e) {
      this.pram.completeTime = e;
    },
    onTypeBlur(e) {
      if (e.target.value) {
        this.pram.dutyName = e.target.value;
        // 如果是对象，要使用this.$set方法
        // this.$set('typeObj', 'name', e.target.value)
      }
    },

    initEditData() {
      this.positionList = this.allPositionList
      this.dutyNameList = this.allDutyNameList

      if (this.isCreate !== 1) return //编辑
      const { matterName, unit, dutiesId, dutyName, completeTime, id, startTime } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.matterName = matterName
      this.pram.unit = unit
      this.pram.dutyName = dutyName
      this.pram.completeTime = completeTime
      this.pram.dutiesId = dutiesId
      this.pram.startTime = startTime
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) {
          return
        }
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      superviseApi.doAdd(this.pram).then(data => {
        this.$message.success('创建督查事项成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      superviseApi.doAdd(this.pram).then(data => {
        this.$message.success('更新督查事项成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>
.tips{
  color:#999;
}
.btn-box{
  margin-top:20px;
}
</style>
